package com.clinic.action.user;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.entity.gui.GuiDoctor;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.logic.user.DoctorLogic;
import com.clinic.util.CommonUtil;

public class DoctorAction extends UserAction {
	
	@Autowired
	private DoctorLogic doctorLogic;
	
	private static final long serialVersionUID = -411877279303429942L;
	private GuiDoctor guiDoctor;
	private GuiPagination guiPagination;
	private List<GuiDoctor> guiDoctorList;
	private String term;
	private SortProperty sort;
	private int page;
	private int size;
	
	public String registration() {
		if(isDoctorLogin() && getLoginUser().isAdmin() == true) {
			return ActionResult.SUCCESS;		
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String show(){
		if (isDoctorLogin() || isAssistantLogin()) {
			guiDoctor = doctorLogic.getDoctorDetailsById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String edit(){
		if(isDoctorLogin() && getLoginUser().isAdmin() == true) {
			guiDoctor = doctorLogic.getDoctorDetailsById(id);
			return ActionResult.SUCCESS;			
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String delete(){
		if(isDoctorLogin() && getLoginUser().isAdmin() == true) {
			guiDoctor = doctorLogic.getDoctorDetailsById(id);
			if(guiDoctor.isAvailable()) {
				doctorLogic.deleteDoctorDetailsById(id);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String update(){
		if(isDoctorLogin() && getLoginUser().isAdmin()) {
			boolean isValidDoctorUpdate = doctorLogic.isValidDoctorUpdate(guiDoctor);
			if(!isValidDoctorUpdate) {
				addActionError("Failed to insert.This docotr's is already registered");							
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiDoctor = doctorLogic.updateDoctor(guiDoctor);  // catch guiDoctor object details returned from inserDoctor()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String insert() {
		if(isDoctorLogin() && getLoginUser().isAdmin() == true) {
			boolean isNotAvailable = doctorLogic.isNotAvailableDoctor(guiDoctor);
			if(!isNotAvailable){
				addActionError("Failed to insert.This docotr's is already registered");
			}
			
			boolean isValidRegNo = doctorLogic.isValidRegNo(guiDoctor);
			if(!isValidRegNo){
				addActionError("Failed to insert.Invalid registration number");
			}
	
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiDoctor = doctorLogic.insertDoctor(guiDoctor);  // catch guiDoctor object details returned from inserDoctor()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String list(){
		if(isDoctorLogin() || isAssistantLogin() || isPatientLogin()) {
			Index index = CommonUtil.getIndexBypage(page);
			if (StringUtils.isNotBlank(term) || sort != null) {
				guiDoctorList = doctorLogic.getDoctorListByTerm(term, sort.getColumn(), index);
				guiPagination = doctorLogic.getPaginationOfDoctorsByTerm(term, sort.getColumn(), page);
				size = guiPagination.getTotalCount();
			} else {
				guiDoctorList = doctorLogic.getDoctorList(index);
				guiPagination = doctorLogic.getPaginationOfDoctors(page);
				size = guiPagination.getTotalCount();
			}		
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;		
	}

	public GuiDoctor getGuiDoctor() {
		return guiDoctor;
	}

	public void setGuiDoctor(GuiDoctor guiDoctor) {
		this.guiDoctor = guiDoctor;
	}

	public DoctorLogic getDoctorLogic() {
		return doctorLogic;
	}

	public void setDoctorLogic(DoctorLogic doctorLogic) {
		this.doctorLogic = doctorLogic;
	}

	public List<GuiDoctor> getGuiDoctorList() {
		return guiDoctorList;
	}

	public void setGuiDoctorList(List<GuiDoctor> guiDoctorList) {
		this.guiDoctorList = guiDoctorList;
	}
	
	public boolean isTermExist() {
		return StringUtils.isNotBlank(term);
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}
	
	public boolean isSortExist() {
		return (sort != null);
	}

	public SortProperty getSort() {
		return sort;
	}
	
	public void setSort(String sortProperty) {
		this.sort = SortProperty.getValue(sortProperty);
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}
	
}
